/*
 * Genera un objeto XMLHttpRequest dependiendo del navegador utilizado
 * Generates an XMLHttpRequest object according to the used web browser
 */
function getXmlHttpObject(){
	var httpRequest=null;
	if (window.XMLHttpRequest){
		// code for IE7+, Firefox, Chrome, Opera, Safari
		httpRequest= new XMLHttpRequest();
	}else if (window.ActiveXObject){
		// code for IE6, IE5
		httpRequest= ActiveXObject("Microsoft.XMLHTTP");
	}
	return httpRequest;
}





var httpRequest;
var elementId;

function getColumnsByTable(id){	
	elementId="refColumns";	
	xmlhttp=getXmlHttpObject();
	if (xmlhttp==null){
	  alert ("Your browser does not support AJAX!");
	  return;
	}
	
	var url="fk.functions.php";
	url+="?function= getColumnsByTable";
	url+="&id="+id;
	url+="&sid="+Math.random();
	xmlhttp.onreadystatechange=stateChangedForRefColumns;
	xmlhttp.open("GET",url,true);
	xmlhttp.send(null);	
}

function stateChangedForRefColumns(){
	if (xmlhttp.readyState==4){
		responseText=xmlhttp.responseText;
		elements=document.getElementsByTagName("select");
		for(i=0;i<elements.length;i++){
			element=elements[i];
			if(element.className==elementId){
				element.innerHTML=responseText;
			}
		}
	}
}

function stateChanged(){
	if (xmlhttp.readyState==4){
		responseText=xmlhttp.responseText;
		document.getElementById(elementId).innerHTML=responseText;
	}
}

function enableRefColumns(checkBox){
	refId='refColumn_'+checkBox.value;
	refColumnSelect=document.getElementById(refId);
	refColumnSelect.disabled=!refColumnSelect.disabled;
}
	
function verificaFormulario(){	
	refTable=document.getElementById("refTable");
	if(refTable.selectedIndex==0){
		alert('Seleccione la tabla de referencia');
		return false;
	}
	
	if(!verificarCheckBox()) return false;
	if(!verificarSelects()) return false;
	return true;
}

function verificarCheckBox(){
	elements=document.getElementsByTagName("input");
	checkedBoxesCount=0;
	for(i=0;i<elements.length;i++){
		element=elements[i];
		if(element.type=="checkbox" && element.checked){
			checkedBoxesCount++;
			refId='refColumn_'+element.value;
			refColumnSelect=document.getElementById(refId);
			if(refColumnSelect.value==''){
				alert('Debe seleccionar la columna de referencia para: '+element.value);
				return false;
			}
		}
	}
	if(checkedBoxesCount==0){
		alert('Debe seleccionar al menos una columna');
		return false;
	}
	return true;
}

function verificarSelects(){
	return true;
}
